import { RCSPManager, RCSPOpDirectoryBrowse, RCSPOpFile, RCSPOpWatchDial } from "../../lib/rcsp-impl/rcsp"
import { OPFile } from "../../jl_lib/jl-rcsp-op/jl_op_watch_1.1.0"

// pages/function_test/index.ts
Page({

    /**
     * 页面的初始数据
     */
    data: {
        devName: ""
    },
    onLoad() {
        const deviceId = RCSPManager.getCurrentRcspOperateWrapper()?.deviceId
        if (deviceId) {
          const blueToothDevice =   RCSPManager.getBluetoothDeviceByDeviceId(deviceId)
            this.setData({
                devName: blueToothDevice?.name
            })
        }
    },
    /**
     * 点击-目录浏览
     */
    clickDirectoryBrowse() {
        if (!RCSPOpDirectoryBrowse) return wx.showToast({ title: "请先连接设备" })
        const storageDevs = RCSPOpDirectoryBrowse.getOnLineStorageDevs()
        if (storageDevs.length == 0) return wx.showToast({ title: "暂无在线的存储设备" })
        const names = new Array<string>()
        storageDevs.forEach(item => {
            const devName = item.getName()
            names.push((devName == undefined) ? "未知设备" : devName)
        })
        return wx.showActionSheet({
            alertText: "选择浏览的设备目录(仅显示在线)",
            itemList: names,
            success(res) {
                const file = storageDevs[res.tapIndex]
                wx.navigateTo({
                    url: '/pages/function_test/directory_browse/index',
                    success: function (res) {
                        // 通过eventChannel向被打开页面传送数据
                        res.eventChannel.emit('acceptDataFromOpenerPage', { data: file })
                    }
                })
            }
        })
    },
    /**
     * 点击-大文件传输(App->Dev)
     */
    clickLargeFileTransformAppToDev() {
        if (!RCSPOpFile) return wx.showToast({ title: "请先连接设备" })
        const storageDevs = RCSPOpFile.getOnLineSdCardBeans()
        if (storageDevs == undefined || storageDevs.length == 0) return wx.showToast({ title: "暂无在线的存储设备" })
        const names = new Array<string>()
        storageDevs.forEach(item => {
            const devName = item.name
            names.push((devName == undefined) ? "未知设备" : devName)
        })
        return wx.showActionSheet({
            alertText: "选择传输的设备(仅显示在线)",
            itemList: names,
            success(res) {
                const file = storageDevs[res.tapIndex]
                wx.navigateTo({
                    url: '/pages/function_test/large_file_transfer/index',
                    success: function (res) {
                        // 通过eventChannel向被打开页面传送数据
                        res.eventChannel.emit('acceptDataFromOpenerPage', { data: file })
                    }
                })
            }
        })
    },
    /**
     * 点击-大文件传输(Dev->App)
     */
    clickLargeFileTransformDevToApp() {
        // wx.showToast({
        //     title: "未实现"
        // })
        wx.navigateTo({
            url:'./large_file_get/index?pagetype=1'
        })
    },
    /**
     * 点击-表盘操作
     */
    clickWatchDialPlateOperate() {
        if (!RCSPOpWatchDial || !RCSPOpFile) return wx.showToast({ title: "请先连接设备" })
        const storageDevs = RCSPOpFile.getOnLineSdCardBeans()
        if (storageDevs) {
            let isHasFlash = false
            for (let index = 0; index < storageDevs.length; index++) {
                const element = storageDevs[index];
                if (element.type == OPFile.SDCardBean.FLASH) {
                    isHasFlash = true
                    break;
                }
            }
            if (isHasFlash) {
                return wx.navigateTo({
                    url: '/pages/function_test/dial_operate/index',
                })
            } else {
                wx.showToast({ title: "暂无在线的Flash存储设备" })
            }
        } else {
            wx.showToast({ title: "暂无在线的存储设备" })
        }
        return
    },
    /**
    * 点击-OTA
    */
    clickOTA() {
        if (!RCSPManager.isConnectedDevce()) {
            wx.showToast({ title: "请先连接设备" })
            return
        }
        wx.navigateTo({
            url: '/pages/function_test/ota/index'
        })
    },
    /**
    * 点击-闹钟
    */
    clickAlarm() {
        if (!RCSPManager.isConnectedDevce()) {
            wx.showToast({ title: "请先连接设备" })
            return
        }
        wx.navigateTo({
            url: '/pages/function_test/pageAlarmClock/pageAlarmClock'
        })
    }
})